package com.zking.dao.domain;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class VerCode {
    private String phoneNum;
    public VerCode(){}
    public VerCode(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String sendCode(String phones){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4GBofNGkGHNfxFLxWgQK", "qtuTGBwY75c1bqDRGkufoq5xC6WAOD");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",phones );
        request.putQueryParameter("SignName", "1001夜餐厅");
        request.putQueryParameter("TemplateCode", "SMS_195860293");

        /*生成随机4位验证码*/
        String code = UUID.randomUUID().toString().substring(0,4);
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            /* System.out.println(response.getData());*/
            return code;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        } catch (com.aliyuncs.exceptions.ServerException e) {
            e.printStackTrace();
            return null;
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String toString() {
        return "VerCode{" +
                "phoneNum='" + phoneNum + '\'' +
                '}';
    }
}
